#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <fcntl.h>

class Util
{
public:
    // XXXX XXX XXX\r\nYYYYY
    static std::string getOneLine(std::string &buffer, const std::string &sep)
    {
        auto pos = buffer.find(sep);
        if (pos == std::string::npos)
            return "";
        std::string sub = buffer.substr(0, pos);
        // buffer.erase(0, sub.size()+sep.size());
        return sub;
    }
    static bool readFile(const std::string resource, std::string* out, int size)
    {
        std::ifstream in(resource, std::ios::binary);// 二进制读是因为可能要打开图片等二进制文件
        // std::ifstream in(resource, std::ios::in);
        if (!in.is_open())
            return false; // resource not found
        std::streamsize sz;
        // 读二进制文件、文本文件都可以
        in.read((char*)out->c_str(), size);

        // 读文本文件
        // std::string line;
        // while(std::getline(in, line))
        // {
        //     *out += line;
        // }

        in.close();
        return true;
    }
};